home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / MDB / Modules / Manager / ibase.php < prev    next >
Encoding:
PHP Script  |  2004-03-24  |  22.6 KB  |  487 lines

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP Version 4                                                        |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1998-2004 Manuel Lemos, Tomas V.V.Cox,                 |
  6. // | Stig. S. Bakken, Lukas Smith                                         |
  7. // | All rights reserved.                                                 |
  8. // +----------------------------------------------------------------------+
  9. // | MDB is a merge of PEAR DB and Metabases that provides a unified DB   |
  10. // | API as well as database abstraction for PHP applications.            |
  11. // | This LICENSE is in the BSD license style.                            |
  12. // |                                                                      |
  13. // | Redistribution and use in source and binary forms, with or without   |
  14. // | modification, are permitted provided that the following conditions   |
  15. // | are met:                                                             |
  16. // |                                                                      |
  17. // | Redistributions of source code must retain the above copyright       |
  18. // | notice, this list of conditions and the following disclaimer.        |
  19. // |                                                                      |
  20. // | Redistributions in binary form must reproduce the above copyright    |
  21. // | notice, this list of conditions and the following disclaimer in the  |
  22. // | documentation and/or other materials provided with the distribution. |
  23. // |                                                                      |
  24. // | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken,    |
  25. // | Lukas Smith nor the names of his contributors may be used to endorse |
  26. // | or promote products derived from this software without specific prior|
  27. // | written permission.                                                  |
  28. // |                                                                      |
  29. // | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS  |
  30. // | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT    |
  31. // | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS    |
  32. // | FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE      |
  33. // | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,          |
  34. // | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
  35. // | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|
  36. // |  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED  |
  37. // | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT          |
  38. // | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|
  39. // | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE          |
  40. // | POSSIBILITY OF SUCH DAMAGE.                                          |
  41. // +----------------------------------------------------------------------+
  42. // | Author: Lorenzo Alberton <l.alberton@quipo.it>                       |
  43. // +----------------------------------------------------------------------+
  44. //
  45. // $Id: ibase.php,v 1.2.4.2 2004/01/08 13:43:01 lsmith Exp $
  46.  
  47. if(!defined('MDB_MANAGER_IBASE_INCLUDED'))
  48. {
  49.     define('MDB_MANAGER_IBASE_INCLUDED', 1);
  50.  
  51. require_once('MDB/Modules/Manager/Common.php');
  52.  
  53. /**
  54.  * MDB FireBird/InterBase driver for the management modules
  55.  *
  56.  * @package MDB
  57.  * @category Database
  58.  * @access private
  59.  * @author  Lorenzo Alberton <l.alberton@quipo.it>
  60.  */
  61. class MDB_Manager_ibase extends MDB_Manager_common
  62. {
  63.     
  64.     // {{{ createDatabase()
  65.  
  66.     /**
  67.      * create a new database
  68.      *
  69.      * @param object $db    database object that is extended by this class
  70.      * @param string $name  name of the database that should be created
  71.      * @return mixed        MDB_OK on success, a MDB error on failure
  72.      * @access public
  73.      **/
  74.     function createDatabase(&$db, $name)
  75.     {
  76.         return ($db->raiseError(MDB_ERROR_UNSUPPORTED, NULL, NULL, 'Create database',
  77.                 'PHP Interbase API does not support direct queries. You have to '
  78.                 .'create the db manually by using isql command or a similar program'));
  79.     }
  80.  
  81.     // }}}
  82.     // {{{ dropDatabase()
  83.  
  84.     /**
  85.      * drop an existing database
  86.      *
  87.      * @param object $db    database object that is extended by this class
  88.      * @param string $name  name of the database that should be dropped
  89.      * @return mixed        MDB_OK on success, a MDB error on failure
  90.      * @access public
  91.      **/
  92.     function dropDatabase(&$db, $name)
  93.     {
  94.         return ($db->raiseError(MDB_ERROR_UNSUPPORTED, NULL, NULL, 'Drop database',
  95.                 'PHP Interbase API does not support direct queries. You have '
  96.                 .'to drop the db manually by using isql command or a similar program'));
  97.     }
  98.  
  99.     // }}}
  100.     // {{{ checkSupportedChanges()
  101.  
  102.     /**
  103.      * check if planned changes are supported
  104.      *
  105.      * @param object $db        database object that is extended by this class
  106.      * @param string $name name of the database that should be dropped
  107.      * @return mixed MDB_OK on success, a MDB error on failure
  108.      * @access public
  109.      **/
  110.     function checkSupportedChanges(&$db, &$changes)
  111.     {
  112.         for($change=0, reset($changes);
  113.             $change<count($changes);
  114.             next($changes), $change++)
  115.         {
  116.             switch(key($changes)) {
  117.                 case "ChangedNotNull":
  118.                 case "notnull":
  119.                     return($this->raiseError(MDB_ERROR_MANAGER, '', '',
  120.                         'Check supported changes: it is not supported changes to field not null constraint'));
  121.                 case "ChangedDefault":
  122.                 case "default":
  123.                     return($this->raiseError(MDB_ERROR_MANAGER, '', '',
  124.                         'Check supported changes: it is not supported changes to field default value'));
  125.                 case "length":
  126.                     return($this->raiseError(MDB_ERROR_MANAGER, '', '',
  127.                         'Check supported changes: it is not supported changes to field default length'));
  128.                 case "unsigned":
  129.                 case "type":
  130.                 case "Declaration":
  131.                 case "Definition":
  132.                     break;
  133.                 default:
  134.                     return($this->raiseError(MDB_ERROR_MANAGER, '', '',
  135.                         'Check supported changes: it is not supported change of type' . key($changes)));
  136.             }
  137.         }
  138.         return(MDB_OK);
  139.     }
  140.  
  141.     // }}}
  142.     // {{{ alterTable()
  143.  
  144.     /**
  145.      * alter an existing table
  146.      *
  147.      * @param object    $db        database object that is extended by this class
  148.      * @param string $name name of the table that is intended to be changed.
  149.      * @param array $changes associative array that contains the details of each type
  150.      *                              of change that is intended to be performed. The types of
  151.      *                              changes that are currently supported are defined as follows:
  152.      *
  153.      *                              name
  154.      *
  155.      *                                 New name for the table.
  156.      *
  157.      *                             AddedFields
  158.      *
  159.      *                                 Associative array with the names of fields to be added as
  160.      *                                  indexes of the array. The value of each entry of the array
  161.      *                                  should be set to another associative array with the properties
  162.      *                                  of the fields to be added. The properties of the fields should
  163.      *                                  be the same as defined by the Metabase parser.
  164.      *
  165.      *                                 Additionally, there should be an entry named Declaration that
  166.      *                                  is expected to contain the portion of the field declaration already
  167.      *                                  in DBMS specific SQL code as it is used in the CREATE TABLE statement.
  168.      *
  169.      *                             RemovedFields
  170.      *
  171.      *                                 Associative array with the names of fields to be removed as indexes
  172.      *                                  of the array. Currently the values assigned to each entry are ignored.
  173.      *                                  An empty array should be used for future compatibility.
  174.      *
  175.      *                             RenamedFields
  176.      *
  177.      *                                 Associative array with the names of fields to be renamed as indexes
  178.      *                                  of the array. The value of each entry of the array should be set to
  179.      *                                  another associative array with the entry named name with the new
  180.      *                                  field name and the entry named Declaration that is expected to contain
  181.      *                                  the portion of the field declaration already in DBMS specific SQL code
  182.      *                                  as it is used in the CREATE TABLE statement.
  183.      *
  184.      *                             ChangedFields
  185.      *
  186.      *                                 Associative array with the names of the fields to be changed as indexes
  187.      *                                  of the array. Keep in mind that if it is intended to change either the
  188.      *                                  name of a field and any other properties, the ChangedFields array entries
  189.      *                                  should have the new names of the fields as array indexes.
  190.      *
  191.      *                                 The value of each entry of the array should be set to another associative
  192.      *                                  array with the properties of the fields to that are meant to be changed as
  193.      *                                  array entries. These entries should be assigned to the new values of the
  194.      *                                  respective properties. The properties of the fields should be the same
  195.      *                                  as defined by the Metabase parser.
  196.      *
  197.      *                                 If the default property is meant to be added, removed or changed, there
  198.      *                                  should also be an entry with index ChangedDefault assigned to 1. Similarly,
  199.      *                                  if the notnull constraint is to be added or removed, there should also be
  200.      *                                  an entry with index ChangedNotNull assigned to 1.
  201.      *
  202.      *                                 Additionally, there should be an entry named Declaration that is expected
  203.      *                                  to contain the portion of the field changed declaration already in DBMS
  204.      *                                  specific SQL code as it is used in the CREATE TABLE statement.
  205.      *                             Example
  206.      *                                 array(
  207.      *                                     'name' => 'userlist',
  208.      *                                     'AddedFields' => array(
  209.      *                                         'quota' => array(
  210.      *                                             'type' => 'integer',
  211.      *                                             'unsigned' => 1
  212.      *                                             'Declaration' => 'quota INT'
  213.      *                                         )
  214.      *                                     ),
  215.      *                                     'RemovedFields' => array(
  216.      *                                         'file_limit' => array(),
  217.      *                                         'time_limit' => array()
  218.      *                                         ),
  219.      *                                     'ChangedFields' => array(
  220.      *                                         'gender' => array(
  221.      *                                             'default' => 'M',
  222.      *                                             'ChangeDefault' => 1,
  223.      *                                             'Declaration' => "gender CHAR(1) DEFAULT 'M'"
  224.      *                                         )
  225.      *                                     ),
  226.      *                                     'RenamedFields' => array(
  227.      *                                         'sex' => array(
  228.      *                                             'name' => 'gender',
  229.      *                                             'Declaration' => "gender CHAR(1) DEFAULT 'M'"
  230.      *                                         )
  231.      *                                     )
  232.      *                                 )
  233.      * @param boolean $check indicates whether the function should just check if the DBMS driver
  234.      *                              can perform the requested table alterations if the value is TRUE or
  235.      *                              actually perform them otherwise.
  236.      * @return mixed MDB_OK on success, a MDB error on failure
  237.      * @access public
  238.      **/
  239.     function alterTable(&$db, $name, &$changes, $check)
  240.     {
  241.         if($check) {
  242.             for($change=0, reset($changes);
  243.                 $change<count($changes);
  244.                 next($changes), $change++)
  245.             {
  246.                 switch(key($changes)) {
  247.                     case "AddedFields":
  248.                     case "RemovedFields":
  249.                     case "RenamedFields":
  250.                         break;
  251.                     case "ChangedFields":
  252.                         $fields = $changes['ChangedFields'];
  253.                         for($field=0, reset($fields);
  254.                             $field<count($fields);
  255.                             next($fields), $field++)
  256.                         {
  257.                             if(MDB::isError($err = $this->checkSupportedChanges($fields[Key($fields)]))) {
  258.                                 return($err);
  259.                             }
  260.                         }
  261.                         break;
  262.                     default:
  263.                         return($this->raiseError(MDB_ERROR_MANAGER, '', '',
  264.                             'Alter table: change type ' . key($changes) . ' not yet supported'));
  265.                 }
  266.             }
  267.             return(MDB_OK);
  268.         } else {
  269.             $query = '';
  270.             if(isset($changes['AddedFields'])) {
  271.                 $fields = $changes['AddedFields'];
  272.                 for($field=0, reset($fields); $field<count($fields); next($fields), $field++) {
  273.                     if(strcmp($query, "")) {
  274.                         $query .= ', ';
  275.                     }
  276.                     $query .= 'ADD ' . $fields[key($fields)]['Declaration'];
  277.                 }
  278.             }
  279.             if(isset($changes['RemovedFields'])) {
  280.                 $fields = $changes['RemovedFields'];
  281.                 for($field=0, reset($fields); $field<count($fields); next($fields), $field++) {
  282.                     if(strcmp($query, "")) {
  283.                         $query .= ', ';
  284.                     }
  285.                     $query .= 'DROP ' . key($fields);
  286.                 }
  287.             }
  288.             if(isset($changes['RenamedFields'])) {
  289.                 $fields = $changes['RenamedFields'];
  290.                 for($field=0, reset($fields); $field<count($fields); next($fields), $field++) {
  291.                     if(strcmp($query, "")) {
  292.                         $query .= ', ';
  293.                     }
  294.                     $query .= 'ALTER ' . key($fields) . ' TO ' . $fields[Key($fields)]['name'];
  295.                 }
  296.             }
  297.             if(isset($changes['ChangedFields'])) {
  298.                 $fields = $changes['ChangedFields'];
  299.                 for($field=0, reset($fields); $field<count($fields); next($fields), $field++) {
  300.                     $field_name = key($fields);
  301.                     if(MDB::isError($err = $this->CheckSupportedChanges($fields[Key($fields)]))) {
  302.                         return($err);
  303.                     }
  304.                     if(strcmp($query, "")) {
  305.                         $query .= ', ';
  306.                     }
  307.                     $query .= 'ALTER '.$field_name.' TYPE '.$db->getFieldDeclaration($fields[$field_name]['Definition']);
  308.                 }
  309.             }
  310.             if(MDB::isError($err = $db->query("ALTER TABLE $name $query"))) {
  311.                 return $err;
  312.             }
  313.             return(MDB_OK);
  314.         }
  315.     }
  316.  
  317.     // }}}
  318.     // {{{ listTableFields()
  319.  
  320.     /**
  321.      * list all fields in a tables in the current database
  322.      *
  323.      * @param object    $db        database object that is extended by this class
  324.      * @param string $table name of table that should be used in method
  325.      * @return mixed data array on success, a MDB error on failure
  326.      * @access public
  327.      */
  328.     function listTableFields(&$db, $table)
  329.     {
  330.         $result = $db->query("SELECT RDB$FIELD_SOURCE FROM RDB$RELATION_FIELDS WHERE RDB$RELATION_NAME='$table'");
  331.         if(MDB::isError($result)) {
  332.             return($result);
  333.         }
  334.         $columns = $db->getColumnNames($result);
  335.         if(MDB::isError($columns)) {
  336.             $db->freeResult($columns);
  337.         }
  338.         return($columns);
  339.     }
  340.  
  341.     // }}}
  342.     // {{{ getTableFieldDefinition()
  343.  
  344.     // }}}
  345.     // {{{ listViews()
  346.  
  347.     /**
  348.      * list the views in the database
  349.      *
  350.      * @param object    $db        database object that is extended by this class
  351.      * @return mixed MDB_OK on success, a MDB error on failure
  352.      * @access public
  353.      **/
  354.     function listViews(&$db)
  355.     {
  356.         //return($db->queryCol('SELECT RDB$VIEW_NAME'));
  357.         return($db->raiseError(MDB_ERROR_UNSUPPORTED, '', '', 'not supported feature'));
  358.     }
  359.  
  360.     // }}}
  361.     // {{{ createIndex()
  362.  
  363.     /**
  364.      * get the stucture of a field into an array
  365.      *
  366.      * @param object    $dbs        database object that is extended by this class
  367.      * @param string    $table         name of the table on which the index is to be created
  368.      * @param string    $name         name of the index to be created
  369.      * @param array     $definition        associative array that defines properties of the index to be created.
  370.      *                                 Currently, only one property named FIELDS is supported. This property
  371.      *                                 is also an associative with the names of the index fields as array
  372.      *                                 indexes. Each entry of this array is set to another type of associative
  373.      *                                 array that specifies properties of the index that are specific to
  374.      *                                 each field.
  375.      *
  376.      *                                Currently, only the sorting property is supported. It should be used
  377.      *                                 to define the sorting direction of the index. It may be set to either
  378.      *                                 ascending or descending.
  379.      *
  380.      *                                Not all DBMS support index sorting direction configuration. The DBMS
  381.      *                                 drivers of those that do not support it ignore this property. Use the
  382.      *                                 function support() to determine whether the DBMS driver can manage indexes.
  383.  
  384.      *                                 Example
  385.      *                                    array(
  386.      *                                        'FIELDS' => array(
  387.      *                                            'user_name' => array(
  388.      *                                                'sorting' => 'ascending'
  389.      *                                            ),
  390.      *                                            'last_login' => array()
  391.      *                                        )
  392.      *                                    )
  393.      * @return mixed MDB_OK on success, a MDB error on failure
  394.      * @access public
  395.      */
  396.     function createIndex(&$db, $table, $name, $definition)
  397.     {
  398.         for($query_sort='', $query_fields='', $field=0, reset($definition['FIELDS']);
  399.             $field<count($definition['FIELDS']);
  400.             $field++, next($definition['FIELDS']))
  401.         {
  402.             if($field > 0) {
  403.                 $query_fields .= ',';
  404.             }
  405.             $field_name = key($definition['FIELDS']);
  406.             $query_fields .= $field_name;
  407.             if(!strcmp($query_sort, "") 
  408.                 && $db->support('IndexSorting')
  409.                 && isset($definition['FIELDS'][$field_name]['sorting']))
  410.             {
  411.                 switch($definition['FIELDS'][$field_name]['sorting']) {
  412.                     case "ascending":
  413.                         $query_sort = " ASC";
  414.                         break;
  415.                     case "descending":
  416.                         $query_sort = " DESC";
  417.                         break;
  418.                 }
  419.             }
  420.         }
  421.         return($db->query('CREATE'.(isset($definition['unique']) ? ' UNIQUE' : '') . $query_sort
  422.                          ." INDEX $name  ON $table ($query_fields)"));
  423.     }
  424.  
  425.     // }}}
  426.     // {{{ createSequence()
  427.  
  428.     /**
  429.      * create sequence
  430.      *
  431.      * @param object    $db        database object that is extended by this class
  432.      * @param string $seq_name name of the sequence to be created
  433.      * @param string $start start value of the sequence; default is 1
  434.      * @return mixed MDB_OK on success, a MDB error on failure
  435.      * @access public
  436.      **/
  437.     function createSequence(&$db, $seq_name, $start)
  438.     {
  439.         $seqname = $db->getSequenceName($seq_name);
  440.         if (MDB::isError($result = $db->query("CREATE GENERATOR $seqname"))) {
  441.             return($result);
  442.         }
  443.         if (MDB::isError($result = $db->query("SET GENERATOR $seqname TO ".($start-1)))) {
  444.             if (MDB::isError($err = $db->dropSequence($seq_name))) {
  445.                 return($this->raiseError(MDB_ERROR_MANAGER, NULL, NULL,
  446.                         'Could not setup sequence start value and then it was not possible to drop it: '
  447.                         .$err->getMessage().' - ' .$err->getUserInfo()));
  448.             }
  449.         }
  450.         return($result);
  451.     }
  452.  
  453.     // }}}
  454.     // {{{ dropSequence()
  455.  
  456.     /**
  457.      * drop existing sequence
  458.      *
  459.      * @param object    $db        database object that is extended by this class
  460.      * @param string $seq_name name of the sequence to be dropped
  461.      * @return mixed MDB_OK on success, a MDB error on failure
  462.      * @access public
  463.      **/
  464.     function dropSequence(&$db, $seq_name)
  465.     {
  466.         $seqname = $db->getSequenceName($seq_name);
  467.         return($db->query("DELETE FROM RDB\$GENERATORS WHERE RDB\$GENERATOR_NAME='".strtoupper($seqname)."'"));
  468.     }
  469.  
  470.     // }}}
  471.     // {{{ listSequences()
  472.  
  473.     /**
  474.      * list all sequences in the current database
  475.      *
  476.      * @param object    $db        database object that is extended by this class
  477.      * @return mixed data array on success, a MDB error on failure
  478.      * @access public
  479.      **/
  480.     function listSequences(&$db)
  481.     {
  482.         return($db->queryCol("SELECT RDB\$GENERATOR_NAME FROM RDB\$GENERATORS"));
  483.     }
  484. }
  485.  
  486. };
  487. ?>